package com.china08.yxyapi.api.home.activity.weike;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.huodong.YxyHuodongWeikeContestCatalogue;
import com.china08.yxyapi.entity.db.res.YxyKnowledge;
import com.china08.yxyapi.entity.db.res.YxyResResource;
import com.china08.yxyapi.entity.db.res.YxyResWeikeKnow;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyHuodongWeikeContestCatalogueRepository;
import com.china08.yxyapi.repository.db.YxyKnowledgeRepository;
import com.china08.yxyapi.repository.db.YxyResResourceRepository;
import com.china08.yxyapi.repository.db.YxyResWeikeKnowRepository;

@CrossOrigin
@RestController
public class ActivityWeikeIssueController extends YxyApiBaseController {

    @Value("${yxy.res.url}")
    private String yxy_res_url;

    @Autowired
    private YxyKnowledgeRepository knowRepository;

    @Autowired
    private YxyResWeikeKnowRepository weikeknowRepository;

    @Autowired
    private YxyHuodongWeikeContestCatalogueRepository yxyHuodongWeikeContestCatalogueRepository;

    @Autowired
    private YxyResResourceRepository yxyResResourceRepository;

    @RequestMapping(value = "/api_yw/home/activity/tea/weike/issue", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(//
            @RequestBody ReqModel model, HttpServletRequest request, //
            HttpServletResponse response//
    ) {
        RespModel respModel = new RespModel();
        YxyHuodongWeikeContestCatalogue one = yxyHuodongWeikeContestCatalogueRepository.findOne(model.catlogId);
        one.setWeikeResId(model.weikeId);
        one.setStatus(true);
        one = yxyHuodongWeikeContestCatalogueRepository.save(one);
        String[] strings = one.getKnowIds().split(",");
        for (String knowThreeId : strings) {
            YxyResWeikeKnow weikeKnow = new YxyResWeikeKnow();
            Integer knoLevel2Id = 0;
            Integer knoLevel1Id = 0;
            Integer knoLevel3Id = 0;
            String level1name = "";
            String level2name = "";
            String level3name = "";
            if ("00".equals(one.getPhaseId())) {
                knoLevel2Id = Integer.valueOf(knowThreeId);
                knoLevel1Id = knoLevel2Id;
                knoLevel3Id = knoLevel2Id;
                level1name = one.getKnowNames();
                level2name = one.getKnowNames();
                level3name = one.getKnowNames();
            } else {
                YxyKnowledge knowThree = knowRepository.findById(Integer.valueOf(knowThreeId));
                level3name = knowThree.getName();
                // 三级
                // 二级
                knoLevel2Id = knowThree.getParentId();
                YxyKnowledge knowTwo = knowRepository.findById(knoLevel2Id);
                level2name = knowTwo.getName();
                // 一级
                knoLevel1Id = knowTwo.getParentId();
                YxyKnowledge knowOne = knowRepository.findById(knoLevel1Id);
                level1name = knowOne.getName();
            }

            weikeKnow.setKnowOneLevelId(knoLevel1Id + "");
            weikeKnow.setKnowOneLevelName(level1name);

            weikeKnow.setKnowTwoLevelId(knoLevel2Id + "");
            weikeKnow.setKnowTwoLevelName(level2name);

            weikeKnow.setKnowId(knoLevel3Id);
            weikeKnow.setKnowName(level3name);
            // 学段,学科
            weikeKnow.setStage(one.getPhaseId());
            weikeKnow.setStageName(one.getPhaseName());
            weikeKnow.setWeikeSubject(one.getSubjectId());
            weikeKnow.setWeikeSubjectName(one.getSubjectName());
            weikeKnow.setWeikeId(model.weikeId);
            weikeKnow.setWeiKeStatus(2);
            weikeknowRepository.save(weikeKnow);
        }
        YxyResResource one2 = yxyResResourceRepository.findOne(model.weikeId);
        one2.setCover(model.cover);
        one2.setIntro(model.intro);
        one2.setIsDelete(0);
        yxyResResourceRepository.save(one2);
        return respModel;
    }

    protected static class ReqModel extends YxyApiReqModel {
        // 微课的封面
        public String cover;
        public String weikeId;
        public String intro;
        public String catlogId;
    }

    protected static class RespModel extends YxyApiRespModel {
    }
}
